$(function () {

    //首次打开页面时获取默认数据
    energyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());

    //选择一个客户
    $("#companyName").dblclick(function () {
        $.baseCustomerApi({
            getBaseValue: function (customer) {
                $("#customerinfoid").val(customer.id);
                $("#companyCode").val(customer.companycode);
                $("#companyName").val(customer.customername);
            },
        });
    });

    //执行查询操作
    $("#searchEnergy").click(function () {
        energyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
    });

    function energyAnalysis(customerinfoid, startDate, endDate) {
        var param = {
            "customerinfoid": customerinfoid,
            "startDate": startDate,
            "endDate": endDate
        };
        $.ajax({
            contentType: "application/json",
            url: "getAllEnergyByDate.shtml",
            data: param,
            async: false,
            dataType: "json",
            success: function (data) {
                //获取所有日期
                var dates = _.uniq(_.sortBy(_.pluck(data, 'summarydate')));
                linear('container', '节电分析表', dates[0] + '~' + dates[dates.length - 1], _.pluck(data, 'summarydate'), '电量 (度)', '度', [{
                    name: '传统灯具耗电量',
                    data: _.pluck(data, 'trapower')
                }, {
                    name: 'LED灯具耗电量',
                    data: _.pluck(data, 'ledpower')
                }], true);
            }
        });
    }
});
